#include "Widget/dbtreewidget.h"

DBTreeWidget::DBTreeWidget(QWidget* parent)
    : QTreeWidget(parent)
{
    iniDBTreeWidget();
    createMenus();
    createActions();

    connect(AppEvent::getInstance(),&AppEvent::sigUpdateDBTreeWidget,
            this,&DBTreeWidget::on_UpdateDBTreeWidget);
    connect(AppEvent::getInstance(),&AppEvent::sigAddNewSQLFLayer,
            this,&DBTreeWidget::on_AddNewSQLFLayer);
    connect(AppEvent::getInstance(),&AppEvent::sigAddGDBData,
            this,&DBTreeWidget::on_AddGDBData);

    connect(this,&DBTreeWidget::sigAddNewLayerToLayersTree,
            AppEvent::getInstance(),&AppEvent::onAddNewLayerToLayersTree);
    connect(this,&DBTreeWidget::sigSendLayerToGPU,
            AppEvent::getInstance(),&AppEvent::onSendLayerToGPU);
    connect(this,&DBTreeWidget::sigUpdateOpengl,
            AppEvent::getInstance(),&AppEvent::onUpdateOpengl);
}

DBTreeWidget::~DBTreeWidget()
{

}

void DBTreeWidget::iniDBTreeWidget()
{
    this->setHeaderHidden(true);
    this->setDropIndicatorShown(true);
    this->setDragEnabled(true);
    this->setDefaultDropAction(Qt::MoveAction);
    this->setDragDropMode(QAbstractItemView::InternalMove);
    this->setSelectionBehavior(QTreeWidget::SelectItems);
    //this->hideColumn(2);
    //this->setAnimated(true);
    //this->setEditTriggers(QTreeWidget::NoEditTriggers);
    //this->setSelectionBehavior(QTreeWidget::SelectRows);
    //this->setAlternatingRowColors(true);
    //this->setFocusPolicy(Qt::NoFocus);
    //this->setContextMenuPolicy(Qt::CustomContextMenu);
    QTreeWidgetItem *rootItem=new QTreeWidgetItem(this);
    rootItem->setIcon(0,QIcon("../Resources/database2.png"));
    rootItem->setText(DBTreeColNum::colItem,QObject::tr("DataBase"));

    mysqlRootItem=new QTreeWidgetItem(DBTreeNodeType::mysqlRoot);
    mysqlRootItem->setIcon(0,QIcon("../Resources/mysql.png"));
    mysqlRootItem->setText(DBTreeColNum::colItem,QObject::tr("MySQL Database"));
    mysqlRootItem->setText(DBTreeColNum::colItemType,QObject::tr("mysqlRoot"));
    //mysqlRootItem->setFlags(Qt::ItemIsSelectable);
    rootItem->addChild(mysqlRootItem);
}


void DBTreeWidget::createMenus()
{
    popMenuOnMySQLRootNode=new QMenu(this);
    popMenuOnDBNode=new QMenu(this);
    popMenuOnFLayerNode=new QMenu(this);
    popMenuOnTableNode=new QMenu(this);
}

void DBTreeWidget::createActions()
{
    actAddDatabase=new QAction(QObject::tr("Add Database"));
    actAddDatabase->setIcon(QIcon("../Resources/add.png"));
    connect(actAddDatabase,&QAction::triggered,
            this,&DBTreeWidget::on_actAddDatabase_triggered);

    actNewDatabase=new QAction(QObject::tr("New Database"));
    connect(actNewDatabase,&QAction::triggered,
            this,&DBTreeWidget::on_actNewDatabase_triggered);

    actImportShpfile=new QAction(QObject::tr("Import Shpfile"));
    actImportShpfile->setIcon(QIcon("../Resources/color/64x64/01.png"));
    connect(actImportShpfile,&QAction::triggered,
            this,&DBTreeWidget::on_actImportShpfile_triggered);

    actImportCSV=new QAction(QObject::tr("Import CSV File"));
    actImportCSV->setIcon(QIcon("../Resources/table.png"));

    actImportProject=new QAction(QObject::tr("Import Project"));
    connect(actImportProject,&QAction::triggered,
            this,&DBTreeWidget::on_actImportProject_triggered);

    actNewFeatureLayer=new QAction(QObject::tr("New FeatureLayer"));
    connect(actNewFeatureLayer,&QAction::triggered,
            this,&DBTreeWidget::on_actNewFeatureLayer_triggered);

    actDeleteFeatureLayer=new QAction(QObject::tr("Delete"));
    actDeleteFeatureLayer->setIcon(QIcon("../Resources/close2.png"));
    connect(actDeleteFeatureLayer,&QAction::triggered,
            this,&DBTreeWidget::on_actDeleteFeatureLayer_triggered);

    actDeleteDatabase=new QAction(QObject::tr("Delete"));
    actDeleteDatabase->setIcon(QIcon("../Resources/close2.png"));
    connect(actDeleteDatabase,&QAction::triggered,
            this,&DBTreeWidget::on_actDeleteDatabase_triggered);

    actRemoveDatabase=new QAction(QObject::tr("Remove"));
    connect(actRemoveDatabase,&QAction::triggered,
            this,&DBTreeWidget::on_actRemoveDatabase_triggered);

    popMenuOnFLayerNode->addAction(actImportProject);
    popMenuOnFLayerNode->addAction(actDeleteFeatureLayer);

    popMenuOnDBNode->addAction(actImportShpfile);
    popMenuOnDBNode->addAction(actImportCSV);
    popMenuOnDBNode->addAction(actNewFeatureLayer);
    popMenuOnDBNode->addAction(actDeleteDatabase);
    popMenuOnDBNode->addAction(actRemoveDatabase);

    popMenuOnMySQLRootNode->addAction(actAddDatabase);
    popMenuOnMySQLRootNode->addAction(actNewDatabase);
}

void DBTreeWidget::mousePressEvent(QMouseEvent *event)
{

    QTreeWidget::mousePressEvent(event);
    QString curDatabase;
    QTreeWidgetItem *curItem=this->currentItem();
    qDebug()<<curItem->type();
    if(curItem->type()==DBTreeNodeType::databaseName)
    {
        curDatabase=curItem->text(0);
        qDebug()<<"curItem->type()==DBTreeNodeType::databaseName";
    }

    if(curItem->type()==DBTreeNodeType::featureGroup
            || curItem->type()==DBTreeNodeType::schemeGroup)
    {
        curDatabase=curItem->parent()->text(0);
        qDebug()<<"curItem->type()==DBTreeNodeType::featureGroup || schemeGroup";
    }
    if(curItem->type()==DBTreeNodeType::FLayerPoint
            ||curItem->type()==DBTreeNodeType::FLayerLine
            ||curItem->type()==DBTreeNodeType::FLayerPolygon
            ||curItem->type()==DBTreeNodeType::Table)
    {
        curDatabase=curItem->parent()->parent()->text(0);
        qDebug()<<"curItem->type()==DBTreeNodeType::FLayerPoint||FLayerLine||FLayerPolygon";
    }
    Env::curDatabase=curDatabase;
    //SQLOperator::getInstance()->changeDatabase(curDatabase);
    /*
    qDebug()<<"enter mousePressEvent.........";
    QTreeWidgetItem *curItem=this->currentItem();

    QString text=curItem->text(DBTreeColNum::colItem);
    qDebug()<<text;
    qDebug()<<curItem->type();

    if(event->button()==Qt::RightButton)
    {
        if(curItem->type()==DBTreeNodeType::mysqlRoot)
        {
            popMenuOnMySQLRootNode->exec(QCursor::pos());
        }
    }
    */

}

void DBTreeWidget::contextMenuEvent(QContextMenuEvent* /*event*/)
{

    qDebug()<<"enter contextMenuEvent.........";
    QTreeWidgetItem *curItem=this->currentItem();
    QString text=curItem->text(DBTreeColNum::colItem);
    qDebug()<<text;
    qDebug()<<curItem->type();

    if(curItem->type()==DBTreeNodeType::mysqlRoot)
    {
        popMenuOnMySQLRootNode->exec(QCursor::pos());
    }
    if(curItem->type()==DBTreeNodeType::databaseName)
    {
        popMenuOnDBNode->exec(QCursor::pos());
    }
    if(curItem->type()==DBTreeNodeType::FLayerPoint
            ||curItem->type()==DBTreeNodeType::FLayerLine
            ||curItem->type()==DBTreeNodeType::FLayerPolygon)
    {
        popMenuOnFLayerNode->exec(QCursor::pos());
    }
}
void DBTreeWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
    QTreeWidget::mouseDoubleClickEvent(event);
}

void DBTreeWidget::on_UpdateDBTreeWidget(const QString &root,QTreeWidgetItem* context)
{
    if(root=="mysqlroot")
    {
        mysqlRootItem->addChild(context);
    }
}

void DBTreeWidget::on_AddNewSQLFLayer(const QString &layerName,GeometryType geomType)
{
    int itemWidth = 40;
    int itemHeight = 25;
    QTreeWidgetItem *dbItem=this->currentItem();
    QTreeWidgetItem *child_featureGroup=dbItem->child(0);
    switch (geomType)
    {
    default:
        break;
    case kPoint:
    {
        SQLOperator::getInstance(Env::curDatabase)->newFeatureLayer(layerName,"POINT",0);
        QTreeWidgetItem *layerItem=new QTreeWidgetItem(DBTreeNodeType::FLayerPoint);
        QPixmap layerTypeRamp(itemWidth, itemHeight);
        layerTypeRamp.fill(QColor(0, 0, 0, 0));
        QPainter paint(&layerTypeRamp);
        paint.setRenderHint(QPainter::Antialiasing, true);
        paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 0, 0, 0));
        QPen pen;
        pen.setWidth(5);
        pen.setColor(QColor(0, 122, 204));
        pen.setStyle(Qt::SolidLine);
        paint.setPen(pen);
        paint.setBrush(QColor(0,122,204));
        paint.drawEllipse(QPointF(itemWidth/2,itemHeight/2),5,5);
        QIcon iconFeatureLayerType;
        iconFeatureLayerType.addPixmap(layerTypeRamp, QIcon::Normal, QIcon::On);
        layerItem->setIcon(0,iconFeatureLayerType);
        layerItem->setText(0,layerName);

        child_featureGroup->addChild(layerItem);
        break;
    }
    case kLineString:
    {
        SQLOperator::getInstance(Env::curDatabase)->newFeatureLayer(layerName,"LINESTRING",0);
        QTreeWidgetItem *layerItem=new QTreeWidgetItem(DBTreeNodeType::FLayerLine);
        QPixmap layerTypeRamp(itemWidth, itemHeight);
        layerTypeRamp.fill(QColor(0, 0, 0, 0));
        QPainter paint(&layerTypeRamp);
        paint.setRenderHint(QPainter::Antialiasing, true);
        paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 0, 0, 0));
        QPen pen;
        pen.setWidth(3);
        pen.setColor(QColor(0, 122, 204));
        pen.setStyle(Qt::SolidLine);
        paint.setPen(pen);
        QLine line(0, itemHeight / 2, itemWidth, itemHeight / 2);
        paint.drawLine(line);

        QIcon iconFeatureLayerType;
        iconFeatureLayerType.addPixmap(layerTypeRamp, QIcon::Normal, QIcon::Off);
        layerItem->setIcon(0,iconFeatureLayerType);
        layerItem->setText(0,layerName);

        child_featureGroup->addChild(layerItem);
        break;
    }
    case kPolygon:
    {
        SQLOperator::getInstance(Env::curDatabase)->newFeatureLayer(layerName,"POLYGON",0);
        QTreeWidgetItem *layerItem=new QTreeWidgetItem(DBTreeNodeType::FLayerPolygon);
        QPixmap layerTypeRamp(itemWidth, itemHeight);
        layerTypeRamp.fill(QColor(0, 0, 0, 0));
        QPainter paint(&layerTypeRamp);
        paint.setRenderHint(QPainter::Antialiasing, true);
        paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 0, 0, 0));

        paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 122, 204));
        QIcon iconFeatureLayerType;
        iconFeatureLayerType.addPixmap(layerTypeRamp, QIcon::Normal, QIcon::On);
        layerItem->setIcon(0,iconFeatureLayerType);
        layerItem->setText(0,layerName);
        child_featureGroup->addChild(layerItem);
        break;
    }
    }

}
void DBTreeWidget::on_actAddDatabase_triggered()
{
    //ConnectMySQLDialog *dialog=new ConnectMySQLDialog();
    QVector<QString> GDBVector=SQLOperator::getInstance(Env::curDatabase)->showDatabases();
    QStringList GDBList;
    for(int i=0;i<GDBVector.size();++i)
    {
        GDBList.append(GDBVector[i]);
    }
    AddGDBDialog *dialog=new AddGDBDialog(GDBList,this);
    dialog->show();
}

void DBTreeWidget::on_actNewDatabase_triggered()
{
    QTreeWidgetItem *itemDBNode=new QTreeWidgetItem(DBTreeNodeType::databaseName);
    itemDBNode->setIcon(0,QIcon("../Resources/database.png"));
    QTreeWidgetItem *itemFLGroupNode=new QTreeWidgetItem(DBTreeNodeType::featureGroup);
    itemFLGroupNode->setIcon(0,QIcon("../Resources/color/64x64/01.png"));
    itemFLGroupNode->setText(0,QObject::tr("Feature Group"));
    QTreeWidgetItem *itemSGroupNode=new QTreeWidgetItem(DBTreeNodeType::schemeGroup);
    itemSGroupNode->setIcon(0,QIcon("../Resources/color/64x64/01.png"));
    itemSGroupNode->setText(0,QObject::tr("Scheme Group"));
    itemDBNode->addChild(itemFLGroupNode);
    itemDBNode->addChild(itemSGroupNode);

    QString dlgTitle=QObject::tr("New Database");
    QString txtLabel=QObject::tr("Input Database Name");
    QString defaultInput=QObject::tr("new Database");
    QLineEdit::EchoMode echoMode=QLineEdit::Normal;
    bool ok=false;
    QString newDatabaseName=QInputDialog::getText(this,dlgTitle,txtLabel,echoMode,defaultInput,&ok);
    if(ok&&!newDatabaseName.isEmpty())
    {
        if(SQLOperator::getInstance(Env::curDatabase)->createDatabase(newDatabaseName))
        {
            Env::curDatabase=newDatabaseName;
            qDebug()<<"current database:"<<Env::curDatabase;
            //
            //if(SQLOperator::getInstance("geo_test")->createBasicsTable())
            if(SQLOperator::getInstance(Env::curDatabase)->createBasicsTable())
            {
                itemDBNode->setText(DBTreeColNum::colItem,newDatabaseName);
                mysqlRootItem->addChild(itemDBNode);
            }
        }
    }
}

void DBTreeWidget::on_actDeleteDatabase_triggered()
{
    QTreeWidgetItem *curItem=currentItem();
    QString DBName=curItem->text(0);
    QTreeWidgetItem *parent=curItem->parent();
    if(SQLOperator::getInstance(Env::curDatabase)->deleteDatabase(DBName))
    {
        parent->removeChild(curItem);
    }
}

void DBTreeWidget::on_actDeleteFeatureLayer_triggered()
{
    QTreeWidgetItem *curItem=currentItem();
    QString FLayerName=curItem->text(0);
    QTreeWidgetItem *parent=curItem->parent();
    if(SQLOperator::getInstance(Env::curDatabase)->dropFeatureLayer(FLayerName))
    {
        parent->removeChild(curItem);
    }
}

void DBTreeWidget::on_actImportProject_triggered()
{
    QTreeWidgetItem *curItem=currentItem();
    QString FLayerName=curItem->text(0);
    GeometryType geomType;
    if(curItem->type()==DBTreeNodeType::FLayerPoint)
    {
        //geomType=GeometryType::kMultiPoint;
        geomType=GeometryType::kPoint;
    }
    if(curItem->type()==DBTreeNodeType::FLayerLine)
    {
        //geomType=GeometryType::kMultiLineString;
        geomType=GeometryType::kLineString;
    }
    if(curItem->type()==DBTreeNodeType::FLayerPolygon)
    {
        //geomType=GeometryType::kMultiPolygon;
        geomType=GeometryType::kPolygon;
    }

    GeoFeatureLayer *newFeatureLayer=SQLOperator::getInstance(Env::curDatabase)->readFeatureLayer(FLayerName,geomType);
    if (!newFeatureLayer)
    {
        QString errmsg = "Read and parse shapefile failed: "+FLayerName;
        QMessageBox::critical(this, "Error", errmsg, QMessageBox::Ok);
    }
    else
    {
        //qDebug()<<"end point x:"<<newFeatureLayer->getEndFeature()->getGeometry()->toLineString()->getEndPoint().x;
        Env::map->addLayer(newFeatureLayer);
        emit sigAddNewLayerToLayersTree(newFeatureLayer);
        emit sigSendLayerToGPU(newFeatureLayer,true);
        emit sigUpdateOpengl();
    }
}

void DBTreeWidget::on_actNewFeatureLayer_triggered()
{
    NewSQLFLayerDialog* dialog = new NewSQLFLayerDialog(this);
    dialog->show();
}

void DBTreeWidget::on_actImportShpfile_triggered()
{
    int itemWidth = 40;
    int itemHeight = 25;
    QTreeWidgetItem *curItem=currentItem();
    QString FLayerName=curItem->text(0);
    QTreeWidgetItem *child_featureGroup=curItem->child(0);
    QStringList files = QFileDialog::getOpenFileNames(
        this, tr("Open File"), "", tr("shapefile(*.shp)"), nullptr,
        QFileDialog::DontUseNativeDialog);

    if (files.isEmpty())
        return;
    //import to database and add dbtreeview node
    for (auto iter = files.begin(); iter != files.end(); ++iter)
    {
        GeoFeatureLayer* newFeatureLayer=SQLOperator::getInstance(Env::curDatabase)->importShpfile(*iter);
        QString layerName=newFeatureLayer->getName();
        switch (newFeatureLayer->getGeometryType())
        {
        default:
            break;
        case kPoint:
        {
            QTreeWidgetItem *layerItem=new QTreeWidgetItem(DBTreeNodeType::FLayerPoint);
            QPixmap layerTypeRamp(itemWidth, itemHeight);
            layerTypeRamp.fill(QColor(0, 0, 0, 0));
            QPainter paint(&layerTypeRamp);
            paint.setRenderHint(QPainter::Antialiasing, true);
            paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 0, 0, 0));
            QPen pen;
            pen.setWidth(5);
            pen.setColor(QColor(0, 122, 204));
            pen.setStyle(Qt::SolidLine);
            paint.setPen(pen);
            paint.setBrush(QColor(0,122,204));
            paint.drawEllipse(QPointF(itemWidth/2,itemHeight/2),5,5);
            QIcon iconFeatureLayerType;
            iconFeatureLayerType.addPixmap(layerTypeRamp, QIcon::Normal, QIcon::On);
            layerItem->setIcon(0,iconFeatureLayerType);
            layerItem->setText(0,layerName);
            child_featureGroup->addChild(layerItem);
            break;
        }
        case kLineString:
        {
            QTreeWidgetItem *layerItem=new QTreeWidgetItem(DBTreeNodeType::FLayerLine);
            QPixmap layerTypeRamp(itemWidth, itemHeight);
            layerTypeRamp.fill(QColor(0, 0, 0, 0));
            QPainter paint(&layerTypeRamp);
            paint.setRenderHint(QPainter::Antialiasing, true);
            paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 0, 0, 0));
            QPen pen;
            pen.setWidth(3);
            pen.setColor(QColor(0, 122, 204));
            pen.setStyle(Qt::SolidLine);
            paint.setPen(pen);
            QLine line(0, itemHeight / 2, itemWidth, itemHeight / 2);
            paint.drawLine(line);

            QIcon iconFeatureLayerType;
            iconFeatureLayerType.addPixmap(layerTypeRamp, QIcon::Normal, QIcon::Off);
            layerItem->setIcon(0,iconFeatureLayerType);
            layerItem->setText(0,layerName);
            child_featureGroup->addChild(layerItem);
            break;
        }
        case kPolygon:
        {
            QTreeWidgetItem *layerItem=new QTreeWidgetItem(DBTreeNodeType::FLayerPolygon);
            QPixmap layerTypeRamp(itemWidth, itemHeight);
            layerTypeRamp.fill(QColor(0, 0, 0, 0));
            QPainter paint(&layerTypeRamp);
            paint.setRenderHint(QPainter::Antialiasing, true);
            paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 0, 0, 0));

            paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 122, 204));
            QIcon iconFeatureLayerType;
            iconFeatureLayerType.addPixmap(layerTypeRamp, QIcon::Normal, QIcon::On);
            layerItem->setIcon(0,iconFeatureLayerType);
            layerItem->setText(0,layerName);
            child_featureGroup->addChild(layerItem);
            break;
        }
        }
    }
}

void DBTreeWidget::on_AddGDBData(QString GDBName)
{
    Env::curDatabase=GDBName;
    QMap<QString,int> layerMap;
    layerMap=SQLOperator::getInstance(Env::curDatabase)->getAllLayer();
    QTreeWidgetItem *itemDBNode=new QTreeWidgetItem(DBTreeNodeType::databaseName);
    itemDBNode->setIcon(0,QIcon("../Resources/database.png"));
    itemDBNode->setText(0,GDBName);
    QTreeWidgetItem *itemFLGroupNode=new QTreeWidgetItem(DBTreeNodeType::featureGroup);
    itemFLGroupNode->setIcon(0,QIcon("../Resources/color/64x64/01.png"));
    itemFLGroupNode->setText(0,QObject::tr("Feature Group"));
    QTreeWidgetItem *itemSGroupNode=new QTreeWidgetItem(DBTreeNodeType::schemeGroup);
    itemSGroupNode->setIcon(0,QIcon("../Resources/color/64x64/01.png"));
    itemSGroupNode->setText(0,QObject::tr("Scheme Group"));
    itemDBNode->addChild(itemFLGroupNode);
    itemDBNode->addChild(itemSGroupNode);
    //qDebug()<<layerMap.keys();
    //qDebug()<<layerMap.values();
    QMap<QString,int>::const_iterator i=layerMap.constBegin();
    int itemWidth = 40;
    int itemHeight = 25;
    /*
    QPixmap layerTypeRamp(itemWidth, itemHeight);
    layerTypeRamp.fill(QColor(0, 0, 0, 0));
    QPainter paint(&layerTypeRamp);
    paint.setRenderHint(QPainter::Antialiasing, true);
    paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 0, 0, 0));
    */
    while(i!=layerMap.constEnd()){
        if(i.value()==1){
            QTreeWidgetItem *itemLayer=new QTreeWidgetItem(DBTreeNodeType::FLayerPoint);
            QPixmap layerTypeRamp(itemWidth, itemHeight);
            layerTypeRamp.fill(QColor(0, 0, 0, 0));
            QPainter paint(&layerTypeRamp);
            paint.setRenderHint(QPainter::Antialiasing, true);
            paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 0, 0, 0));
            QPen pen;
            pen.setWidth(5);
            pen.setColor(QColor(0, 122, 204));
            pen.setStyle(Qt::SolidLine);
            paint.setPen(pen);
            paint.setBrush(QColor(0,122,204));
            paint.drawEllipse(QPointF(itemWidth/2,itemHeight/2),5,5);
            QIcon iconFeatureLayerType;
            iconFeatureLayerType.addPixmap(layerTypeRamp, QIcon::Normal, QIcon::On);
            itemLayer->setIcon(0, iconFeatureLayerType);
            itemLayer->setText(0,i.key());
            itemFLGroupNode->addChild(itemLayer);
            //qDebug()<<"POINT:"<<i.key();
        }
        if(i.value()==2){
            QTreeWidgetItem *itemLayer=new QTreeWidgetItem(DBTreeNodeType::FLayerLine);
            QPixmap layerTypeRamp(itemWidth, itemHeight);
            layerTypeRamp.fill(QColor(0, 0, 0, 0));
            QPainter paint(&layerTypeRamp);
            paint.setRenderHint(QPainter::Antialiasing, true);
            paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 0, 0, 0));
            QPen pen;
            pen.setWidth(3);
            pen.setColor(QColor(0, 122, 204));
            pen.setStyle(Qt::SolidLine);
            paint.setPen(pen);
            QLine line(0, itemHeight / 2, itemWidth, itemHeight / 2);
            paint.drawLine(line);

            QIcon iconFeatureLayerType;
            iconFeatureLayerType.addPixmap(layerTypeRamp, QIcon::Normal, QIcon::Off);
            itemLayer->setIcon(0, iconFeatureLayerType);
            itemLayer->setText(0,i.key());
            itemFLGroupNode->addChild(itemLayer);
            //qDebug()<<"LINE:"<<i.key();
        }
        if(i.value()==3){
            QTreeWidgetItem *itemLayer=new QTreeWidgetItem(DBTreeNodeType::FLayerPolygon);
            QPixmap layerTypeRamp(itemWidth, itemHeight);
            layerTypeRamp.fill(QColor(0, 0, 0, 0));
            QPainter paint(&layerTypeRamp);
            paint.setRenderHint(QPainter::Antialiasing, true);
            paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 0, 0, 0));

            paint.fillRect(QRect(0, 0, itemWidth, itemHeight), QColor(0, 122, 204));
            QIcon iconFeatureLayerType;
            iconFeatureLayerType.addPixmap(layerTypeRamp, QIcon::Normal, QIcon::On);
            itemLayer->setIcon(0, iconFeatureLayerType);
            itemLayer->setText(0,i.key());
            itemFLGroupNode->addChild(itemLayer);
        }
        ++i;
    }

    mysqlRootItem->addChild(itemDBNode);
}


void DBTreeWidget::on_actRemoveDatabase_triggered()
{
    QTreeWidgetItem *curItem=currentItem();
    QString DBName=curItem->text(0);
    QTreeWidgetItem *parent=curItem->parent();
    parent->removeChild(curItem);
}








